#include <iostream>
#include <memory>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;

const char* host = "118.178.95.219";
const char* user = "lin_zizhuo";
const char* passwd = "@Lzz1935738";
const unsigned int port = 3306;


int main()
{
    try {
        /* Create a connection */
        unique_ptr<sql::Driver> driver(get_driver_instance());
        unique_ptr<sql::Connection> con(driver->connect("tcp://118.178.95.219:3306", "lin_zizhuo", "@Lzz1935738"));

        /* Connect to the MySQL test database */
        con->setSchema("test_db");
        con->setClientOption("charset_name", "utf8");

        unique_ptr<sql::Statement> stmt(con->createStatement());
        unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM test1"));

        sql::ResultSetMetaData* meta = res->getMetaData();
        int colnum = meta->getColumnCount();

        for (int i = 1; i <= colnum; ++i)
            cout << meta->getColumnName(i) << ' ';
        cout << endl;

        while (res->next()) {

            cout << "ID: " << res->getInt("id");
            cout << ", Name: " << res->getString("name");
            cout << ", tele: " << res->getString("telephone") << endl;
        }

    }
    catch (sql::SQLException& e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

    return 0;
}
